package cxm.proj.sms;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ControlFragment extends Fragment implements
		OnItemSelectedListener, OnClickListener, TextWatcher {
	class ConfigSpinnerItem {
		public Configure.ConfigureOption option;
		String optionName;
		
		public ConfigSpinnerItem(String optionName, Configure.ConfigureOption option) {
			this.option = option;
			this.optionName = optionName;
		}
		
		@Override
		public String toString() {
		    return this.optionName;
		}
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_control, container, false);
        
        // bind button click
        Button btn = (Button)view.findViewById(R.id.button_set);
        btn.setOnClickListener(this);
        btn = (Button)view.findViewById(R.id.button_reset);
        btn.setOnClickListener(this);
        
        // bind text changed
        EditText editText = (EditText)view.findViewById(R.id.phone);
        editText.addTextChangedListener(this);
        
        // Inflate the layout for this fragment
        // create adapter for spinner
		ArrayAdapter<ConfigSpinnerItem> myAdapter = new ArrayAdapter<ConfigSpinnerItem>(
				this.getActivity(), android.R.layout.simple_spinner_item);
		myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Configure.ConfigureOption []options = Configure.ConfigureOption.values();
		// remove Status config
		ArrayList<Configure.ConfigureOption> list = new ArrayList<Configure.ConfigureOption>();
		for (int i = 0; i < options.length; i++)
			if ((options[i].toString().contains("Toggle") &&
					(options[i].toString().contains("Open") ||
							options[i].toString().contains("Close") ||
							options[i].toString().contains("Text"))) ||
							options[i].toString().contains("PhoneNumber"))
				list.add(options[i]);
		// get option show name from string const
		String []optionNames = this.getActivity().getResources().getStringArray(R.array.array_optionsname);
		for (int i = 0; i < list.size(); i++)
			myAdapter.add(new ConfigSpinnerItem(optionNames[i], list.get(i)));

		// set adapter for spinner
		Spinner spinner = (Spinner) view.findViewById(R.id.planets_spinner);
		spinner.setAdapter(myAdapter);
		spinner.setOnItemSelectedListener(this);
		
		return view;
    }
	
	@Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) 
    {
		Spinner spinner = (Spinner)arg0;
		ConfigSpinnerItem configItem = (ConfigSpinnerItem)spinner.getAdapter().getItem(arg2);
		// config edit text
		EditText editText = (EditText)this.getView().findViewById(R.id.phone);
		if (configItem.option == Configure.ConfigureOption.PhoneNumber) {
			editText.setHint(this.getActivity().getString(R.string.phone_hint));
			editText.setInputType(InputType.TYPE_CLASS_PHONE);
		}
		else {
			if (configItem.option.toString().contains("Text")) 
				editText.setHint(this.getActivity().getString(R.string.controlcode_hinttext));
			else
				editText.setHint(this.getActivity().getString(R.string.controlcode_hint));
			editText.setInputType(InputType.TYPE_CLASS_TEXT);
		}
		// get configure from file
		editText.setText(Configure.getInstance().getConfigString(configItem.option));
		Button button = (Button)this.getView().findViewById(R.id.button_set);
		button.setEnabled(false);
		button = (Button)this.getView().findViewById(R.id.button_reset);
		button.setEnabled(true);
    }
	
	@Override
    public void onNothingSelected(AdapterView<?> arg0) 
    {
        // Remain empty
    }
	
	@Override
	public void onClick(View v) {
		// get config option to set
		Spinner spinner = (Spinner)this.getActivity().findViewById(R.id.planets_spinner);
		int selectItem = spinner.getSelectedItemPosition();
	    if (Spinner.INVALID_POSITION == selectItem) {
	    	Log.d("control", "User not set option");
	    	return;
	    }
	    ConfigSpinnerItem configItem = (ConfigSpinnerItem)spinner.getAdapter().getItem(selectItem);
	    String setValue;
		if (v.getId() == R.id.button_set) {
			setValue = ((EditText)this.getActivity().findViewById(R.id.phone)).getText().toString();
		} else {
			setValue = Configure.getInstance().getDefaultConfigString(configItem.option);
			EditText editText = (EditText)this.getView().findViewById(R.id.phone);
			editText.setText(setValue);
		}
		// set value
		try {
	    	Configure.getInstance().setConfig(configItem.option, setValue);
	    } catch (Exception e) {
	    	new AlertDialog.Builder(this.getActivity()).setTitle("Error").setMessage(
	    			e.toString()).setPositiveButton("Yes", null).show();
	    }
		// mark button unreachable
		v.setEnabled(false);
	}
	
	@Override
	public void afterTextChanged(Editable s) {
		// remain empty
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		// remain empty
	}
	
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		this.getView().findViewById(R.id.button_set).setEnabled(true);
		this.getView().findViewById(R.id.button_reset).setEnabled(true);
	}
}
